package com.tianshi.product.controller;


import com.tianshi.vo.ProductVO;
import com.tianshi.entity.Products;
import com.tianshi.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    //根据id获取商品信息
    @GetMapping("/{productId}")
    public ResponseEntity<ProductVO> getProductByProductId(@PathVariable("productId") Long id) {
        log.info("获取的商品id为："+id);
        ProductVO product = productService.getProductById(id);
        log.info("商品为:{}"+product);
        return ResponseEntity.ok(product);
    }

    //查询所有商品信息
    @GetMapping("/all")
    public ResponseEntity<List<ProductVO>> getAllProducts() {
        log.info("查询所有商品信息");
        List<ProductVO> products = productService.getAllProducts();
        return ResponseEntity.ok(products);
    }

    //根据商品名称模糊查询商品信息
    @GetMapping("/search/{productName}")
    public ResponseEntity<List<ProductVO>> searchProductByName(@PathVariable("productName") String productName) {
        return ResponseEntity.ok(productService.searchProductByName(productName));
    }
    //新增商品信息
    @PostMapping("/add")
    public ResponseEntity<Products> addProduct(@RequestBody Products product) {
        log.info("新增商品成功{}", product);
        Long result = productService.addProduct(product);
        if (result > 0) {
            return ResponseEntity.ok(product);
        }
        return ResponseEntity.badRequest().body(null);
    }

    //修改商品信息
    @PutMapping("/update")
    public ResponseEntity<String> updateProduct(@RequestBody Products product) {
        Products result = productService.updateProduct(product);
        log.info("修改商品成功");
        if (result != null) {
            return ResponseEntity.ok().body("修改成功");
        }
        return ResponseEntity.badRequest().body("error");
    }

    //删除商品信息
    @DeleteMapping("/delete/{productId}")
    public ResponseEntity<String> deleteProduct(@PathVariable("productId") Long id) {
        Long result = productService.deleteProduct(id);
        log.info("删除商品成功");
        if (result > 0) {
            return ResponseEntity.ok().body("删除成功");
        }
        return ResponseEntity.badRequest().body("删除失败");
    }

    //根据商品名称模糊查询商品
    @GetMapping("/searchByKeyword/{keyword}")
    public ResponseEntity<List<Products>> searchByKeyword(@PathVariable String keyword) {
        System.out.println("搜索关键字为："+keyword);
        List<Products> products = productService.searchByKeyword(keyword);
        System.out.println("搜索到的商品为："+products);
        return ResponseEntity.ok(products);
    }
    //根据分类查询商品
    @GetMapping("/searchByCategory/{categoryId}")
    public ResponseEntity<List<Products>> searchByCategory(@PathVariable Long categoryId) {
        System.out.println("搜索分类id为："+categoryId);
        List<Products> products = productService.searchByCategory(categoryId);
        System.out.println("搜索到的商品为："+products);
        return ResponseEntity.ok(products);
    }
    //获取热门商品
    @GetMapping("/hot")
    public ResponseEntity<List<Products>> getHotProducts() {
        List<Products> products = productService.getHotProducts();
        System.out.println("热门商品为："+products);
        return ResponseEntity.ok(products);
    }
}
